/* eslint-disable camelcase */
"use strict";

const path = require("node:path");
const process = require("node:process");
const { RsdoctorRspackPlugin } = require("@rsdoctor/rspack-plugin");
const { defineConfig } = require("@rspack/cli");
const { CopyRspackPlugin } = require("@rspack/core");
const {
  BannerPlugin,
  applyRspackOptionsBaseDefaults,
} = require("@rspack/core");
const { RspackManifestPlugin } = require("rspack-manifest-plugin");
const { merge } = require("webpack-merge");
const base = require("./rspack.config.cjs");

const isDevelopment = process.env.NODE_ENV !== "production";
/**
 * @param {string[]} p
 * @returns {string}
 */
const pwd2 = (...p) => path.resolve(process.cwd(), ...p);

const build = defineConfig({
  devtool: "cheap-module-source-map",
  plugins: [
    Number(process.env.RP_RSDOCTOR) &&
      new RsdoctorRspackPlugin({
        port: 6899,
        mode: "normal",
        linter: {
          rules: {
            "ecma-version-check": "off",
          },
        },
      }),
    new RspackManifestPlugin({
      fileName: "manifest.json",
    }),
    // new BannerPlugin({
    //   banner: "@ts-nocheck",
    // }),
    // 只复制一次
    isDevelopment &&
      new CopyRspackPlugin({
        patterns: [
          {
            from: pwd2("./node_modules/@uirouter/visualizer/images"),
            to: pwd2("./public/images"),
          },
        ],
      }),
  ],
  output: {
    clean: true,
  },
  optimization: {
    minimize: false,
    innerGraph: true,
    splitChunks: {
      maxInitialRequests: 30,
      chunks: "initial",
      usedExports: true,
      cacheGroups: {
        // ng_module: {
        //   test: /[\\/]node_modules[\\/]angular-([a-zA-Z]+)(-[a-zA-Z]+)?[\\/]/,
        //   name: "ng-module-chunk",
        //   reuseExistingChunk: true,
        //   chunks: "initial",
        // },
        d3_module: {
          test: /[\\/]node_modules[\\/]d3[\\/]/,
          name: "d3-chunk",
          reuseExistingChunk: true,
          filename: "d3~main-[contenthash:8].js",
          chunks: "async",
        },
        router_core: {
          test: /[\\/]node_modules[\\/]@uirouter[\\/]core[\\/]/,
          name: "router-core-chunk",
          filename: "uirouter~core-[contenthash:8].js",
          chunks: "initial",
        },
        router_main: {
          test: /[\\/]node_modules[\\/]@uirouter[\\/]angularjs[\\/]/,
          name: "router-main-chunk",
          filename: "uirouter~main-[contenthash:8].js",
          chunks: "initial",
        },
        echarts_core: {
          test: /[\\/]node_modules[\\/]echarts[\\/]core/,
          name: "echarts-core-chunk",
          reuseExistingChunk: true,
          chunks: "async",
        },
        zrender_core: {
          test: /[\\/]node_modules[\\/]zrender[\\/]core/,
          name: "zrender-core-chunk",
          reuseExistingChunk: true,
          chunks: "async",
        },
        // tslib: {
        //   test:  /[\\/]node_modules[\\/]tslib[\\/]/,
        //   name: "tslib-initial-chunk",
        //   reuseExistingChunk: true,
        //   chunks: "all",
        // },
        // reflect_metadata: {
        //   test:  /[\\/]node_modules[\\/]reflect-metadata[\\/]/,
        //   name: "reflect_metadata-initial-chunk",
        //   reuseExistingChunk: true,
        //   chunks: "all",
        // },
      },
    },
  },
});

/**
 * @type {import("@rspack/cli").Configuration}
 */
module.exports =
  // process.env.NODE_ENV === 'development' ?
  merge(base, build);
// : base;
